package com.lanwf.admin.data.product;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.lanwf.admin.BaseController;
import com.lanwf.IAdminIndexController;
import com.lanwf.admin.sys.PermissionService;
import com.lanwf.common.model.Product;
import com.lanwf.admin.utils.CommonUtil;
import com.lanwf.admin.utils.Cons;
import com.lanwf.admin.utils.Result;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Auther: Lanwf
 * @Date: 2022/05/21 10:55
 * @Description:
 */
@Path(value = "/admin/product", viewPath = "/admin/data/product")
public class  ProductAdminController extends BaseController implements IAdminIndexController {

    @Inject
    PermissionService permissionService;

    @Override
    public void index() {
        set("permission",permissionService.getPermission(getAccountId(),getInt(0)));
        render("index.html");
    }

    @Override
    public void paginate() {
        Integer p = getInt("pageNum",1);
        Integer s = getInt("pageSize",10);

        String orderByColumn = get("orderByColumn", "");
        String isAsc = get("isAsc", "");

        String stitle = get("stitle","").trim();
        String slinkman = get("slinkman","").trim();
        String semail = get("semail","").trim();
        Integer type = getInt("type",0);

        List<Object> param = new ArrayList<>();
        String sql = " from product where 1=1 ";

        if(StrKit.notBlank(stitle)){
            sql += " and title like ? or subtitle like ? ";
            param.add("%"+stitle+"%");
            param.add("%"+stitle+"%");
        }
        if(StrKit.notBlank(slinkman)){
            sql += " and linkman like ? ";
            param.add("%"+slinkman+"%");
        }
        if(StrKit.notBlank(semail)){
            sql += " and email like ? ";
            param.add("%"+semail+"%");
        }

        if(type > 0){
            sql += " and type = ? ";
            param.add(type);
        }

        sql += " order by ";
        if(StrKit.notBlank(orderByColumn)){
            sql += ""+orderByColumn + " "+isAsc;
        }

        Page<Product> paginate = Product.dao.paginate(p, s, "select *",  sql,param.toArray());
        renderJson(new Result<>(paginate.getTotalRow(),paginate.getList()));
    }

    @Override
    public void add() {
        render("add_edit.html");
    }

    @Override
    public void save() {
        UploadFile filepath = getFile("filepath",Cons.HISTORY_IMG);
        Product model = getModel(Product.class, "product");
        if(filepath != null){
            model.setCoverImg(CommonUtil.getFilePath(filepath,Cons.HISTORY_IMG));
        }
        model.set("create_time",new Date()).set("update_time",new Date()).save();
        renderJson(Ret.ok().set("msg","添加成功"));
    }

    @Override
    public void edit() {
        Product product = Product.dao.findById(getInt(0));
        if(StringUtils.isNotBlank(product.getImgList())){
            String[] split = StringUtils.split(product.getImgList(), ",");
            ArrayList<Object> imgList = new ArrayList<>();
            imgList.addAll( Arrays.asList(split));
            product.put("imgList",imgList);
        }
        set("product",product);
        render("add_edit.html");
    }

    @Override
    public void update() {
        UploadFile filepath = getFile("filepath",Cons.HISTORY_IMG);

        Product model = getModel(Product.class, "product");
        if(filepath != null){
            //CommonUtil.deleteFile(Product.dao.findById(model.getId()).getCoverImg());
            model.setCoverImg(CommonUtil.getFilePath(filepath,Cons.HISTORY_IMG));
        }

        model.set("update_time",new Date()).update();
        renderJson(Ret.ok().set("msg","修改成功"));
    }

    @Override
    public void delete() {
        Integer id = getInt(0);
        Product product = Product.dao.findById(id);
        CommonUtil.deleteFile(product.getCoverImg());
        Product.dao.deleteById(id);
        renderJson(Ret.ok().set("msg","删除成功"));
    }
}
